/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: lz4_decoder.c
 *
 * Purpose: implementation a decoder with lz4 decompress
 *
 * Developer:
 *   wen.gu , 2022-03-04
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include "lz4_decoder.h"

/******************************************************************************
 **    MACROS
 ******************************************************************************/
#define LOGE printf
#define LOGD printf
/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/

/******************************************************************************
 **    inner FUNCTION DEFINITIONS
 ******************************************************************************/
static uint32_t get_frame_size(kfa_raw_type raw_type, uint32_t width, uint32_t height)
{
    switch (raw_type)
    {
    case kfa_raw_type::RGBA32: return width * height * 4;     break;
    case kfa_raw_type::RGB24:  return width * height * 3;     break;
    case kfa_raw_type::NV12:   return width * height * 3 / 2; break;
    default: break;
    }

    return 0;
}
/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
Lz4Decoder::Lz4Decoder()
{
    decoder_ = LZ4_createStreamDecode();
}

Lz4Decoder::~Lz4Decoder()
{
    if (decoder_)
    {
        LZ4_freeStreamDecode(decoder_);
    }

    freeOutBuffer();

}

bool Lz4Decoder::setConfigure(KfaDecoderParam& param)
{    
    if (!is_initialzied_)
    {
        freeOutBuffer();
        uint32_t out_buf_size = get_frame_size(param.raw_type, param.pic_width, param.pic_height);
        allocOutBuffer(out_buf_size);        
    }

    return true;
}

bool Lz4Decoder::decode(const uint8_t* in_data, uint32_t in_size, KfaBuffer& out_buf)
{
    uint8_t* buf = out_buf_[out_idx_++ % LZ4_DEC_OUT_BUF_CNT];

    //LOGD("got buffer, capacity: %d, in packet size: %d, raw size: %d\n", buf->capacity(), in_pkt.size, in_pkt.raw_size);

    const int decBytes = LZ4_decompress_safe_continue(decoder_, (const char*)in_data, (char*)buf, in_size, out_size_);

    if (decBytes <= 0)
    {
        LOGD("[%s.%d]===>: %d\n", __FUNCTION__, __LINE__, decBytes);
        out_idx_--;
        return false;
    }     
    //LOGD("[%s.%d]===>:\n", __FUNCTION__, __LINE__);
    out_buf.buf = buf;
    out_buf.size = decBytes;
    out_buf.release = [](KfaBuffer& org_buf)
    {
        //todo something
    };

    //LOGD("[%s.%d]===>:\n", __FUNCTION__, __LINE__);
    return true;
}

void Lz4Decoder::freeOutBuffer()
{
    for (size_t i = 0; i < LZ4_DEC_OUT_BUF_CNT; i++)
    {
        if (out_buf_[i])
        {
            delete[] out_buf_[i];
        }
    }
}

void Lz4Decoder::allocOutBuffer(uint32_t buffer_size)
{
    for (size_t i = 0; i < LZ4_DEC_OUT_BUF_CNT; i++)
    {
        out_buf_[i] = new uint8_t[buffer_size];
    }    

    out_size_ = buffer_size;
}